package com.zw.transition.util;

import com.zw.transition.constant.Constant;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * @Description: Servlet 工具类
 *
 * @Author: China.sgl
 * @Date: 2020/7/30 9:42
 */
@Log4j2
@SuppressWarnings("all")
public class ServletUtils {

    /**
     * 获取参数
     *
     * @param name 参数名
     * @return 参数值
     */
    public static String getParameter(String name) {
        return getRequest().getParameter(name);
    }

    /**
     * 获取Integer参数
     *
     * @param name 参数名
     * @return 参数值
     *
     */
    public static Integer getIntParameter(String name) {
        String value = getParameter(name);
        if (null == value) {
            return null;
        }
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取 ServletRequestAttributes
     *
     * @return attributes
     */
    public static ServletRequestAttributes getRequestAttributes() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        return (ServletRequestAttributes) attributes;
    }

    /**
     * 获取response
     */
    public static HttpServletResponse getResponse() {
        return getRequestAttributes().getResponse();
    }

    /**
     * 获取session
     */
    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    /**
     * 获取request
     */
    public static HttpServletRequest getRequest() {
        return getRequestAttributes().getRequest();
    }

    /**
     * 将字符串渲染到客户端
     *
     * @param response response
     * @param string string
     */
    public static String renderString(HttpServletResponse response, String string) {
        try {
            response.setContentType(Constant.CONTENT_TYPE_JSON);
            response.setCharacterEncoding("utf-8");
            response.getWriter().write(string);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 是否是Ajax异步请求
     *
     * @param request 请求对象
     */
    public static boolean isAjaxRequest(HttpServletRequest request) {
        String accept = request.getHeader(Constant.HEAD_ACCEPT);
        if (accept != null && accept.contains(Constant.CONTENT_TYPE_JSON)) {
            return true;
        }
        String xRequestedWith = request.getHeader(Constant.HEAD_X);
        if (xRequestedWith != null && xRequestedWith.contains(Constant.HEAD_X_XML)) {
            return true;
        }
        String uri = request.getRequestURI();
        if (CommonUtils.containsIgnoreCase(uri, Constant.SUFFIX_JSON, Constant.SUFFIX_XML)) {
            return true;
        }
        String ajax = request.getParameter("__ajax");
        if (ajax!=null){
            return CommonUtils.containsIgnoreCase(ajax, Constant.PATTERN_JSON, Constant.PATTERN_XML);
        }
        return false;
    }
}
